<?php
namespace app\app\controller;
use think\Controller;
use think\Route;
class Resource extends Controller
{
    public function index()
    {
        $keyword = input('get.keyword');
        if ($keyword == ''){
            $resource_list = db('resource')->paginate(8);
        }else {
            $resource_list = db('resource')->where('resource','like','%'.$keyword.'%')->paginate(8);
        }
        $page = $resource_list->render();
        
//         $resource_list = $resource_list->toArray()['data'];
//         foreach ($resource_list as $rk => $rv){
//         	$rv['path'] = urlencode(ROOT_PATH.'Uploads/resource/'.$rv['path']);
// //         	Route::resource('resource',ROOT_PATH.'Uploads/resource/');
        	
//         }
        
        
        
        
        $talk = new Talk();
        $if_login = $talk->check_login();
        if ($if_login == false){
            $this->assign('login_status','never');
        }else {
            $talk = new Talk();
            $never_read_num = $talk->never_read();
            $this->assign('never_read_num',$never_read_num);
            $this->assign('user_name',session('user_name','','app'));
            $this->assign('user_id',session('user_id','','app'));
            $this->assign('login_status','have');
            $this->assign('logout_url',url('index.php/app/talk/logout'));
            $this->assign('personal_url',url('index.php/app/main/personal_set'));
        }
        $this->assign('resource_list',$resource_list);
        $this->assign('page',$page);
        $this->assign('title','资料下载');
        $this->assign('active','resource');
        return $this->fetch('index');
    }
    public function download_file()
    {
        $file_path = input('get.path');
        $file_path = trim($file_path);
        if ($file_path == ''){
            return false;
        }
        $file_path = ROOT_PATH.'Uploads'.DS.'resource'.DS.$file_path;
        $this->download($file_path);
    }
    
    
//     protected function download($file_url,$new_name=''){
//         if(!isset($file_url)||trim($file_url)==''){
//             echo '500';
//         }
//         if(!file_exists($file_url)){ //检查文件是否存在
//             echo '404';
//         }
//         $file_name=basename($file_url);
//         $file_type=explode('.',$file_url);
//         $file_type=$file_type[count($file_type)-1];
//         $file_name=trim($new_name=='')?$file_name:urlencode($new_name);
//         $file_type=fopen($file_url,'r'); //打开文件
//         //输入文件标签
//         header("Content-type: application/octet-stream");
//         header("Accept-Ranges: bytes");
//         header("Accept-Length: ".filesize($file_url));
//         header("Content-Disposition: attachment; filename=".$file_name);
//         //输出文件内容
//         echo fread($file_type,filesize($file_url));
//         fclose($file_type);
//     }

    
    
     public  function download($file_url,$new_name=''){
     	$file_name = explode("\\",$file_url);
     	$file_name = end($file_name);
        $file_url=iconv('utf-8','gb2312',$file_url);
        
        //将编码转为支持中英文的gb2312编码
        if(!isset($file_url)||trim($file_url)==''){
            echo  '500';
            die();
        }
        if(!file_exists($file_url)){ //检查文件是否存在
            echo '404';
            die();
        }
        $file_type=explode('.',$file_url);
        $file_type=$file_type[count($file_type)-1];
        $file_name=trim($new_name)==''?$file_name:urlencode($new_name).'.'.$file_type;
        
        ob_clean();
        //输入文件标签
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length: ".filesize($file_url));
        header("Content-Disposition: attachment; filename=".$file_name);
       
        $file_type=fopen($file_url,'r'); //打开文件
        //输出文件内容
        $file_size=filesize($file_url);//获取文件大小
        $buffer=1024;   //定义1KB的缓存空间
        $file_count=0;  //计数器,计算发送了多少数据
        while(!feof($file_type) && ($file_size>$file_count)){
            //如果文件还没读到结尾，且还有数据没有发送
            $senddata=fread($file_type,$buffer);
            //读取文件内容到缓存区
            $file_count+=$senddata;
            echo $senddata;
        }
        
        flush();
      
//         echo fread($file_type,filesize($file_url));
        fclose($file_type);
        exit();
    }
    
}